package com.ruoyi.cms.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.cms.domain.bo.CmsWebsiteBo;
import com.ruoyi.cms.domain.vo.CmsWebsiteVo;
import com.ruoyi.cms.service.ICmsWebsiteService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;

/**
 * 站点
 *
 * @author guorui
 * @date 2023-11-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cms/website")
public class CmsWebsiteController extends BaseController {

    private final ICmsWebsiteService iCmsWebsiteService;

    /**
     * 查询站点列表
     */
    @SaCheckPermission("cms:website:list")
    @GetMapping("/list")
    public TableDataInfo<CmsWebsiteVo> list(CmsWebsiteBo bo, PageQuery pageQuery) {
        return iCmsWebsiteService.queryPageList(bo, pageQuery);
    }
    
    @SaCheckPermission("cms:website:list")
    @GetMapping("/listAll")
    public R<List<CmsWebsiteVo>> listAll() {
    	List<CmsWebsiteVo> resultList = iCmsWebsiteService.findAll();
    	return R.ok(resultList);
    }

    /**
     * 导出站点列表
     */
    @SaCheckPermission("cms:website:export")
    @Log(title = "站点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CmsWebsiteBo bo, HttpServletResponse response) {
        List<CmsWebsiteVo> list = iCmsWebsiteService.queryList(bo);
        ExcelUtil.exportExcel(list, "站点", CmsWebsiteVo.class, response);
    }

    /**
     * 获取站点详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cms:website:query")
    @GetMapping("/{id}")
    public R<CmsWebsiteVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iCmsWebsiteService.queryById(id));
    }

    /**
     * 新增站点
     */
    @SaCheckPermission("cms:website:add")
    @Log(title = "站点", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CmsWebsiteBo bo) {
        return toAjax(iCmsWebsiteService.insertByBo(bo));
    }

    /**
     * 修改站点
     */
    @SaCheckPermission("cms:website:edit")
    @Log(title = "站点", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CmsWebsiteBo bo) {
        return toAjax(iCmsWebsiteService.updateByBo(bo));
    }

    /**
     * 删除站点
     *
     * @param ids 主键串
     */
    @SaCheckPermission("cms:website:remove")
    @Log(title = "站点", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iCmsWebsiteService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
